package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingDto;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "后台预约设置接口")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile, HttpServletResponse response) {
        //读取execl文件
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(excelFile.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //读取第一张表
        XSSFSheet sheet = workbook.getSheetAt(0);

    /*@Autowired
    private OrderSettingService orderSettingService;

    //预约管理首页展示
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year,Integer month){
        log.info("[接收到的年份是：]", year);
        List<OrderSetting> riqi = orderSettingService.selectAll(year,month);
        //封装返回结果
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,riqi);
    }
        */
    //批量上传日历预约信息
    /*@PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile) throws IOException {
        //读取execl文件
        InputStream inputStream = excelFile.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        //读取第一张表
        XSSFSheet sheet = workbook.getSheetAt(0);

        //遍历每一个单元格，将一行数据装入一个String类型的集合中，再将数组装入集合中
        List<ArrayList<String>> sheetData = new ArrayList<>();
        //循环每一行
        for (int rowIndex = 1; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
            Row row = sheet.getRow(rowIndex);

            //存放每一行的数据
            ArrayList<String> rowData = new ArrayList<>();
            //循环每一列
            for (int cellIndex = 0; cellIndex < row.getPhysicalNumberOfCells(); cellIndex++) {
                //得到每一个单元格
                Cell cell = row.getCell(cellIndex);
                //处理日期
                switch (cellIndex) {
                    case 0:
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        rowData.add(sdf.format(cell.getDateCellValue()));
                        break;
                    case 1:
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        rowData.add(cell.getStringCellValue());
                        break;
                }
            }
            sheetData.add(rowData);
        }

        System.out.println(sheetData);
        orderSettingService.addBatch(sheetData);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }*/

        //遍历每一个单元格，将一行数据装入一个String类型的集合中，再将数组装入集合中
        List<ArrayList<String>> sheetData = new ArrayList<>();
        //循环每一行
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            //存放每一行的数据
            ArrayList<String> rowData = new ArrayList<>();
            //循环每一列
            for (int i1 = 0; i1 < row.getPhysicalNumberOfCells(); i1++) {
                //得到每一个单元格
                Cell cell = row.getCell(i1);
                //处理日期
                switch (i1) {
                    case 0:
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        rowData.add(sdf.format(cell.getDateCellValue()));
                        break;
                    case 1:
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        rowData.add(cell.getStringCellValue());
                        break;
                }
            }
            sheetData.add(rowData);
        }
        orderSettingService.add(sheetData);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     *
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year, Integer month) {
        log.info("查询时间：" + year + "年" + month + "月");
        List<OrderSettingDto> orderSettingList = orderSettingService.getOrderSettingByMonth(year, month);
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, orderSettingList);
    }
    /**
     *
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
}
